package com.cd.music.interceptor;

import com.cd.music.annotation.Auth;
import com.cd.music.dto.ResultBean;
import com.cd.music.utils.CookieUtil;
import com.cd.music.utils.StringUtil;
import com.cd.music.utils.TokenUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;

/**
 * @author yxl 余秀良
 * @version 1.0
 * @description: 权限验证注解实现
 * @date 2018年03月13日  下午2:29
 * @address: 四川成都
 */
@Scope(value = "singleton")
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            // 获取方法上的注解
            Auth annotation = method.getAnnotation(Auth.class);
            // 如果方法上的注解为空 则获取类的注解
            if (annotation == null) {
                annotation = method.getDeclaringClass().getAnnotation(Auth.class);
            }
            //没有注解不做处理
            if (annotation == null) {
                return true;
            }
            //查询用户的权限
            ResultBean resultBean = CookieUtil.get("token");
            String token = resultBean.getData();
            String roles = annotation.roles();
            if (!StringUtil.isEmpty(token)) {
                //获取用户的角色
                Map userInfo = TokenUtil.getParameter(token, Map.class);
                try {
                    int sign = Integer.parseInt(userInfo.get("sign") + "");
                    int[] number = annotation.sign();
                    Arrays.sort(number);
                    int i = Arrays.binarySearch(number, sign);
                    //有权限
                    if (i >= 0) {
                        return true;
                    } else {
                        //没有权限
                        redirect(httpServletResponse, roles);
                        return false;
                    }
                } catch (NumberFormatException e) {
                    return false;
                }
            } else {
                redirect(httpServletResponse, roles);
            }
        } else {
            return true;
        }
        return false;
    }

    /**
     * 重定向页面
     *
     * @param httpServletResponse 　HttpServletResponse
     * @param roles               　角色
     * @throws IOException 　异常
     */
    private void redirect(HttpServletResponse httpServletResponse, String roles) throws IOException {
        String remark = "admin";
        if (roles.contains(remark)) {
            httpServletResponse.sendRedirect("/admin/index.html");
        } else {
            httpServletResponse.sendRedirect("/home/login");
        }
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
